package logic

import (
	"fmt"
	"context"

	"e4ting/internal/model"
	"e4ting/internal/svc"
	"e4ting/pb/e4ting"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTypeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTypeLogic {
	return &GetTypeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetTypeLogic) GetType(in *e4ting.ID) (*e4ting.OneTypeRes, error) {
	var err error
	var item model.Types
	fmt.Printf("name=%v, id=%v", in.Name, in.Value)
	if in.Value == 0 {
		item,err = l.svcCtx.TypeModel.FindOneByName(l.ctx, in.Name)
	} else {
		item,err = l.svcCtx.TypeModel.FindOne(l.ctx, int64(in.Value))
	}
	if err != nil {
		return &e4ting.OneTypeRes{
			Code: 404,
			Err: err.Error(),
		}, nil
	}
	fmt.Printf("%+v\n", item)
	return &e4ting.OneTypeRes{
		Code   : 200,
		Data   : ToTypeData(item),
	}, nil
}
